package ee.example.final_work

import android.os.Build
import android.os.Bundle
import android.view.View
import android.view.Window
import android.view.WindowManager
import android.widget.LinearLayout
import android.widget.TabHost.TabSpec
import android.widget.TextView
import androidx.annotation.RequiresApi
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.FragmentTabHost
import com.example.final_work.R
import ee.example.final_work.Data.initView

class MainActivity : AppCompatActivity() {
    private val mBundle = Bundle()
    private var mTabHost: FragmentTabHost? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        requestWindowFeature(Window.FEATURE_NO_TITLE)
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
        initView()
        setContentView(R.layout.activity_main)
        val mBundle = Bundle()
        mBundle.putString("tag", TAG)

        //初始化tabHost
        mTabHost = findViewById<View>(R.id.tabhost) as FragmentTabHost
        //将tabHost和FrameLayout关联
        mTabHost!!.setup(applicationContext, supportFragmentManager, R.id.tabcontent_page)

        //添加tab和其对应的fragment
        //addTab(标签，跳转的Fragment，传递参数的Bundle)
        mTabHost!!.addTab(
            getTabView(R.string.tab_home, R.drawable.shouye_2),
            TabFragment_Home::class.java,
            mBundle
        )
        mTabHost!!.addTab(
            getTabView(R.string.tab_find, R.drawable.faxian),
            TabFragment_Find::class.java,
            mBundle
        )
        mTabHost!!.addTab(
            getTabView(R.string.tab_community, R.drawable.shequ),
            TabFragment_Community::class.java,
            mBundle
        )
        mTabHost!!.addTab(
            getTabView(R.string.tab_me, R.drawable.wode_1),
            TabFragment_Me::class.java,
            mBundle
        )
        //设置tabs之间的分隔线不显示
        mTabHost!!.tabWidget.showDividers = LinearLayout.SHOW_DIVIDER_NONE
    }

    private fun getTabView(textId: Int, imgId: Int): TabSpec {
        val text = resources.getString(textId)
        val drawable = resources.getDrawable(imgId)
        //必须设置图片大小，否则不显示
        drawable.setBounds(0, 0, drawable.minimumWidth, drawable.minimumHeight)
        val view_tabitem = layoutInflater.inflate(R.layout.item_tabbar, null)
        val tv_item =
            view_tabitem.findViewById<View>(R.id.tv_item_tabbar) as TextView
        tv_item.text = text
        tv_item.setCompoundDrawables(null, drawable, null, null)
        return mTabHost!!.newTabSpec(text).setIndicator(view_tabitem)
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    private fun setStatusBar() {
        //getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
//注意要清除 FLAG_TRANSLUCENT_STATUS flag
        //getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        window.statusBarColor =
            resources.getColor(R.color.xml_color) //设置要显示的颜色（Color.TRANSPARENT为透明）
    }

    companion object {
        private const val TAG = "MainActivity"
    }
}
